#! /usr/bin/env python

##########################################################################################################################
# this file is used as a stand alone application to create a report about versions of system/python/application versions #
##########################################################################################################################

########################################################
# Part of MILIM NIRDAFOT project.
# Written by H. Magal, 2014
# See details on the project at the site:
#   https://sites.google.com/site/nirdafotdownloads/home
########################################################

import sys
import os
import platform
import md5

# user modules
import synonymDB
import constants

wxIsFound = None
try:
    import wx
    wxIsFound = True
except:    # catch all errors. It might be that wxPython was not installed
    wxIsFound = False

def createVersionsString ():
    s = '============ System ============'
    platfrm = sys.platform
    s += '\nPlatform: ' + platfrm
    platfrm = platform.uname()         # get full platform details
    s1 = '\nSystem: ' + platfrm[0] + '\nRelease: ' + platfrm[2] + '\nVersion: ' + platfrm[3] + '\nMachine: ' + platfrm[4] + '     Processor: ' + platfrm[4]
    s += s1
    ds = ('Unknown','Unknown')
    if wxIsFound:
        ds = wx.DisplaySize()   # get screen dimensions
    height = ds [1]
    width = ds [0]
    s1 = '\nDisplay resolution: ' + repr(ds[0]) + 'X' + repr(ds[1]) + ' (wXh)'
    s += s1
    s += '\n============ Python ============'
    s1 = sys.version
    s2 = s1.replace ('\n', ' ')        # replace all new lines by spaces
    pyversion = s2.replace ('\r', ' ') # replace all RETURNs by spaces
    if wxIsFound:
        wv = wx.VERSION
        wxv = repr(wv[0]) + "." + repr(wv[1]) + "." + repr(wv[2]) + "." + repr(wv[3])       # form wxPython version
    else:
        wxv = 'wxPython cannot be found!'
    s1 = '\nPython version: ' + pyversion + '\nwxPython version: ' + wxv
    s += s1
    s += '\n============ Application ============'
    s1 = '\nEngine version: ' + constants.__version__
    db = synonymDB.synonymDB()      # create data base
    s2 = '\nDataBase version: ' + db.version
    s = s + s1 + s2
    s1 = '\nNode: ' + platform.node ()
    s2 = '\nCWDu: ' + os.getcwdu ()
    s = s + s1 + s2

    md = md5.new()
    md.update (platform.node())             # add node name to MD5
    md.update (':')
    md.update (os.getcwdu())                # add CWD() to MD5
    s1 = '\nLicense File: ' + 'lic.' + md.hexdigest () + '.txt'
    s = s + s1
    return s


def versions2File (filename):
    s = createVersionsString ()
    tech = open (filename, 'w')          # create output file
    tech.write (s)
    tech.close()


###### Main code ######
if __name__ == '__main__':
    if wxIsFound:
        app = wx.App()                        # this is required in order to get screen resolution.
    versions2File ('version.sa.txt')
    print 'File version.sa.txt was written !'